package alarm_swing;

import javax.swing.JOptionPane;
import java.io.IOException;
import java.net.URI;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.OutputStream;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3976271981201427578L;
	private JPanel pan = new JPanel();// 创建一个面板
	private JLabel namelab = new JLabel("用户名");
	private JLabel passlab = new JLabel("密    码");
	private JTextField nametext = new JTextField();
	private JPasswordField passtext = new JPasswordField();
	private static final String BASE_URL = "https://app5541.acapp.acwing.com.cn/api/"; // 替换为实际的远程API地址

	public JButton denglu = new JButton("登录");
	public JButton zhuce = new JButton("注册");
	public JButton updatepass = new JButton("修改密码");
	public JButton deleteuser = new JButton("删除用户");

	public Login() {
		Font font = new Font("宋体", Font.BOLD, 12);
		super.setTitle("欢迎登录本系统");
		pan.setLayout(null);
		namelab.setBounds(20, 20, 60, 30);
		nametext.setBounds(90, 20, 140, 30);
		passlab.setBounds(20, 60, 60, 30);
		passtext.setBounds(90, 60, 140, 30);
		denglu.setBounds(30, 120, 90, 20);
		zhuce.setBounds(140, 120, 90, 20);
		updatepass.setBounds(30, 150, 90, 20);
		deleteuser.setBounds(140, 150, 90, 20);

		pan.add(namelab);
		pan.add(nametext);
		pan.add(passlab);
		pan.add(passtext);
		pan.add(denglu);
		pan.add(zhuce);
		pan.add(updatepass);
		pan.add(deleteuser);

		passtext.setFont(font);
		zhuce.setFont(font);
		updatepass.setFont(font);
		deleteuser.setFont(font);

		denglu.addActionListener(this);
		zhuce.addActionListener(this);
		updatepass.addActionListener(this);
		deleteuser.addActionListener(this);

		super.add(pan);
		super.setSize(300, 250);
		super.setVisible(true);
	}

	public static void main(String[] args) {

		new Login();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == denglu) {
			denglu();
		} else if (arg0.getSource() == zhuce) {
			zhuce();
		} else if (arg0.getSource() == updatepass) {
			updatepass();
		} else if (arg0.getSource() == deleteuser) {
			deleteuser();
		}

	}

	// 登录按钮的事件处理函数

	public void denglu() {
		String username = nametext.getText();
		String password = passtext.getText();

		// 发送远程请求进行登录验证
		if (remoteAuthentication(username, password)) {
			JOptionPane.showMessageDialog(null, "登录成功！");
			super.setVisible(false);
		} else {
			JOptionPane.showMessageDialog(null, "用户名或密码错误！");
		}
	}

	// 远程注册按钮触发后的事件处理函数
	public void zhuce() {
		String username = nametext.getText();
		String password = passtext.getText();

		// 发送远程请求进行注册
		if (remoteRegistration(username, password)) {
			JOptionPane.showMessageDialog(null, "注册成功！");
		} else {
			JOptionPane.showMessageDialog(null, "注册失败，请重试！");
		}
	}

	// 发送远程请求进行登录验证
	private boolean remoteAuthentication(String username, String password) {
		// 构建请求的URL
		String url = BASE_URL + "user/login/";

		// 构建请求参数
		Map<String, String> params = new HashMap<>();
		params.put("username", username);
		params.put("password", password);

		try {
			// 创建URL对象
			URL apiUrl = new URL(url);

			// 打开连接
			HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setDoOutput(true);

			// 构建请求体
			String requestBody = buildRequestBody(params);

			// 发送请求
			try (OutputStream outputStream = connection.getOutputStream()) {
				byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
				outputStream.write(requestBodyBytes);
			}

			// 接收响应
			int responseCode = connection.getResponseCode();

			// 处理响应
			if (responseCode == 200) {
				// 登录成功
				return true;
			} else {
				// 登录失败
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}


	// 发送远程请求进行注册
	private boolean remoteRegistration(String username, String password) {
		// 构建请求的URL
		String url = BASE_URL + "user/register/";

		// 构建请求参数
		Map<String, String> params = new HashMap<>();
		params.put("username", username);
		params.put("password", password);

		try {
			// 创建URL对象
			URL apiUrl = new URL(url);

			// 打开连接
			HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setDoOutput(true);

			// 构建请求体
			String requestBody = buildRequestBody(params);

			// 发送请求
			try (OutputStream outputStream = connection.getOutputStream()) {
				byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
				outputStream.write(requestBodyBytes);
			}

			// 接收响应
			int responseCode = connection.getResponseCode();

			// 处理响应
			if (responseCode == 200) {
				// 注册成功
				return true;
			} else {
				// 注册失败
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}




	// 构建表单数据
	private String buildRequestBody(Map<String, String> data) {
		StringBuilder builder = new StringBuilder();
		builder.append("{");
		for (Map.Entry<String, String> entry : data.entrySet()) {
			if (builder.length() > 1) {
				builder.append(",");
			}
			builder.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
		}
		builder.append("}");
		return builder.toString();
	}


	// 修改密码按钮触发后的事件处理函数
	public void updatepass() {
		pan.setEnabled(false);
		JFrame frame1 = new JFrame("密码修改");
		frame1.setSize(250, 200);
		JPanel updatepass = new JPanel();
		JLabel namelab1 = new JLabel("用户名");
		JLabel passlab1 = new JLabel("旧密码");
		JLabel newpasslab = new JLabel("新密码");
		JTextField nametext1 = new JTextField("" + nametext.getText());
		JPasswordField passtext1 = new JPasswordField();
		JPasswordField newpasstext = new JPasswordField();
		JButton ok = new JButton("修改");
		JButton resert = new JButton("重置");

		updatepass.setLayout(null);

		namelab1.setBounds(5, 5, 70, 20);
		nametext1.setBounds(80, 5, 120, 20);
		passlab1.setBounds(5, 30, 70, 20);
		passtext1.setBounds(80, 30, 120, 20);
		newpasslab.setBounds(5, 60, 70, 20);
		newpasstext.setBounds(80, 60, 120, 20);
		ok.setBounds(10, 110, 60, 20);
		resert.setBounds(90, 110, 60, 20);

		updatepass.add(namelab1);
		updatepass.add(nametext1);
		updatepass.add(passlab1);
		updatepass.add(passtext1);
		updatepass.add(newpasslab);
		updatepass.add(newpasstext);
		updatepass.add(ok);
		updatepass.add(resert);

		frame1.add(updatepass);
		frame1.setVisible(true);

		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
//				Jdbc d = new Jdbc();
//				String username = nametext1.getText();
//				String password1 = passtext1.getText();
//				String newpassword = newpasstext.getText();
//				if (d.update(username, password1, newpassword)) {
//					frame1.setVisible(false);
//				}
			}
		});
		// 重置文本框 里的内容
		resert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				nametext1.setText("");
				passtext1.setText("");
				newpasstext.setText("");
			}
		});
	}

	// 删除用户按钮触发后的事件处理函数
	public void deleteuser() {
//		String username = nametext.getText();
//		String password = passtext.getText();
//		Jdbc s = new Jdbc();
//		s.delete(username, password);
	}
}
